const express = require("express")
var fs = require('fs');
var path = require('path')


const {
  checkToken
} = require("../tool/jwt.js")
const {
  find
} = require("../mod/rank")
const home = express.Router() //创建路由
const Rank = require("../mod/rank")
const Sentences = require("../mod/sentences")

//查询背景图片库的图片名数组
home.get("/getBcgList", async (req, res) => {
  let filePath = path.join(__dirname, )
  filePath = filePath.replace("router", "public/bck")
  fs.readdir(filePath, (err, files) => {
    if (err) {
      return res.status(500).json({
        message: "获取失败，刷新重试"
      })
    }

    res.status(200).json({
      data: files
    })

  })

})

//查询轻音乐库的歌曲数组
home.get("/getMusicNumber", async (req, res) => {
  let filePath = path.join(__dirname, )
  filePath = filePath.replace("router", "public/music")

  let data =await fs.readdirSync(filePath)
  // console.log(data)
  if (!data) {
    return res.status(500).json({
      message: "获取失败，刷新重试"
    })
  }

  data = data.filter(item => {
    let num = item.indexOf(".mp3")
    if (num != -1) {
      return item
    }

  })

  res.status(200).json({
    data
  })

})


//每日金句
home.get("/sentence", async (req, res) => {
  let count = await Sentences.countDocuments()
  let number = Math.floor(Math.random() * count)
  let datalist = await Sentences.findOne({
    number
  })
  // console.log(data)
  if (datalist) {
    return res.status(200).json({
      datalist,
      message: "ok258"
    })
  }

  res.status(201).json({
    message: "每日寄语获取失败，点击重新获取"
  })

})

//rank关键字搜索,分页显示
home.get("/rankkeywords", async (req, res) => {
  let {
    num,
    pageSize,
    user_id,
    keywords
  } = req.query
  // console.log(keywords)
  let arr = keywords.trim()
  arr = arr.split(" ")
  // console.log(arr)
  num = num - 0
  pageSize = pageSize - 0
  let skip = pageSize * (num - 1)
  // keywords = new RegExp(keywords, "i") //i 忽略大小写
  let total = await Rank.countDocuments({
    user_id,
    $or: [{
      test_name: {
        $regex: arr[0]
      }
    }]
  })

  // console.log(total)
  let datalist = await Rank.find({
    user_id,
    $or: [{
      test_name: {
        $regex: arr[0]
      }
    }]
  }).skip(skip).limit(pageSize).sort({
    test_time: 0
  })

  if (datalist) {
    return res.status(200).json({
      total,
      datalist,
      message: "ok"
    })
  }

  res.status(201).json({
    message: "获取失败，请重试"
  })

})

//成绩页面的echarts数据
home.get("/rankEcharts", async (req, res) => {
  //用户id 年级 科目 类型
  let datalist = await Rank.find(req.query)
  if (datalist) {
    return res.status(200).json({
      message: "ok",
      datalist
    })
  }

  res.status(201).json({
    message: "err"
  })


})

//提交修改的成绩
home.put("/putRank", async (req, res) => {
  // console.log(req.body)
  let {
    _id
  } = req.body
  req.body.push_time = Date.now() + ""
  let data = await Rank.updateOne({
    _id
  }, req.body)
  if (data) {
    return res.status(200).json({
      message: '更新成功'
    })
  }

  res.status(201).json({
    message: '更新err'
  })
})

//根据id获取一条成绩
home.get("/Rank", async (req, res) => {
  let {
    _id
  } = req.query
  let data = await Rank.findOne({
    _id
  })
  if (data) {
    res.status(200).json({
      data,
      message: "获取ok"
    })
  } else {
    res.status(201).json({
      message: "获取err"
    })
  }
})

//删除一条上传成绩
home.delete("/rankdel", async (req, res) => {
  //需要的参数 成绩 _id
  let {
    _id
  } = req.query
  // console.log(_id)
  _id = _id + ""
  let data = await Rank.remove({
    _id
  })
  //  console.log(data)
  if (data) {
    res.status(200).json({
      message: "删除ok"
    })
  } else {
    res.status(201).json({
      message: "删除err"
    })
  }
})

//分页实现
home.get("/", async (req, res) => { //必须要先匹配到/home ，才能来这儿
  // console.log(req.query)
  let {
    num,
    pageSize,
    test_grade,
    test_project,
    test_type,
    user_id
  } = req.query

  test_grade = test_grade - 0
  test_project = test_project - 0
  test_type = test_type - 0
  num = num - 0
  pageSize = pageSize - 0
  let skip = pageSize * (num - 1)

  var datalist = []
  var total = 0
  datalist = await Rank.find({
    test_grade,
    test_type,
    test_project,
    user_id
  }).skip(skip).limit(pageSize)

  // console.log(datalist)
  total = await Rank.countDocuments({
    user_id,
    test_grade,
    test_type,
    test_project
  })

  res.status(200).json({
    message: "获取数据ok",
    datalist,
    total
  })

})



module.exports = home